//
//  HWRecommendVc.m
//  JWC
//
//  Created by lanou on 16/4/8.
//  Copyright © 2016年 陈帅. All rights reserved.
//

#import "HWRecommendVc.h"
#import "HWMainTableViewCell.h"
#import "HWConcernTool.h"
#import "HWCategoryModel.h"
#import "HWMainModel.h"
#import "MJRefresh.h"
#import "HWRecommendDetailVc.h"

@interface HWRecommendVc ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet UITableView *categoryTable;
@property (weak, nonatomic) IBOutlet UITableView *mainTable;
@property (nonatomic, strong)HWConcernTool *tool;
@property (nonatomic, strong)NSArray *categoryArray;
@property (nonatomic, strong)NSMutableArray *mainArray;
@property (nonatomic, assign)NSInteger page;

@end

@implementation HWRecommendVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setNavBar];
    [self setupTableView];
    [self setupRefreshView];
    
    // 加载左边栏数据
    [self.tool getCategoryData:^(NSArray *listArray) {
        self.categoryArray = listArray;
        [self.categoryTable reloadData];
    }];
}

#pragma mark --- 导航栏 ---
- (void)setNavBar
{
    self.navigationItem.title = @"推荐关注";
    self.view.backgroundColor = BackgroundColor;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.leftBtn];
}

- (UIButton *)leftBtn
{
    if (_leftBtn == nil)
    {
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftBtn setBackgroundImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [_leftBtn setBackgroundImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        [_leftBtn addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _leftBtn.size = _leftBtn.currentBackgroundImage.size;
    }
    return _leftBtn;
}

- (void)backButtonAction:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark --- 设置tableView ---
- (void)setupTableView
{
    self.categoryTable.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    self.mainTable.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    
    self.mainTable.rowHeight = 70;
    self.categoryTable.backgroundColor = BackgroundColor;
    // 去掉不要的cell
    self.categoryTable.tableFooterView = [[UIView alloc]init];
    // 注册tableView
    [self.mainTable registerNib:[UINib nibWithNibName:@"HWMainTableViewCell" bundle:nil] forCellReuseIdentifier:@"main"];
    [self.categoryTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"category"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.categoryTable)
    {
        return self.categoryArray.count;
    }
    else
    {
        return self.mainArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.categoryTable)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"category"];
        HWCategoryModel *model = _categoryArray[indexPath.row];
        cell.textLabel.text = model.name;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        
        return cell;
    }
    else
    {
        HWMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"main"];
        HWMainModel *model = self.mainArray[indexPath.row];
        [cell setMainWithModel:model];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.categoryTable)
    {
        [self.mainTable.mj_header beginRefreshing];
        
        HWCategoryModel *model = self.categoryArray[self.categoryTable.indexPathForSelectedRow.row];
        NSInteger myID = [model.id integerValue];
        //    NSNumber *num = [NSNumber numberWithInteger:myID];
        [self.tool getMainDataWithID:myID block:^(id json) {
            self.mainArray = json;
            [self.mainTable reloadData];
            [self.mainTable.mj_header endRefreshing];
        }];
    }
    else
    {
        HWMainModel *model = self.mainArray[indexPath.row];
        HWRecommendDetailVc *detailVc = [[HWRecommendDetailVc alloc]init];
        detailVc.model= model;
        [self.navigationController pushViewController:detailVc animated:YES];
    }
    
}

#pragma mark --- 下拉刷新 ---
- (void)setupRefreshView
{
    self.mainTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNew)];
    [self.mainTable.mj_header beginRefreshing];
    self.mainTable.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMore)];
    self.mainTable.mj_footer.hidden = YES;
}

- (void)getNew
{
    _page = 1;
    HWCategoryModel *model = self.categoryArray[self.categoryTable.indexPathForSelectedRow.row];
    NSInteger myID = [model.id integerValue];
//    NSNumber *num = [NSNumber numberWithInteger:myID];
    [self.tool getMainDataWithID:myID block:^(id json) {
        self.mainArray = json;
        [self.mainTable reloadData];
        [self.mainTable.mj_header endRefreshing];
    }];
}

- (void)getMore
{
    _page += 1;
    HWCategoryModel *model = self.categoryArray[self.categoryTable.indexPathForSelectedRow.row];
    NSInteger myID = [model.id integerValue];
//    NSNumber *num = [NSNumber numberWithInteger:myID];
    [self.tool getMainDataWithID:myID currentPage:_page block:^(id json) {
        [self.mainArray addObjectsFromArray:json];
        [self.mainTable reloadData];
        [self.mainTable.mj_footer endRefreshing];
    }];
}

#pragma mark --- getter 和 setter
- (HWConcernTool *)tool
{
    if (_tool == nil)
    {
        _tool = [[HWConcernTool alloc]init];
    }
    return _tool;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
